<?php

/**
 * @author Thibaut Cromphaut, Gaïtano Boeraeve, Mathias Helin, Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent 
 */

class Application_Model_DepartmentHasSubdepartmentMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Subdepartments';
        
    }

    /**
     * subdepartements met een bepaalde department_id ophalen
     * 
     * @param integer $id
     * @return Application_Model_Course
     */
    /*public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('departments_id = ?' => $id));

        return $this->_departmentToSubdepartment($row);
    }*/
    
    public function fetchAllWithDepartmentId($id)
    {        
        
        $rows = $this->getDbTable()->fetchAll(array('department_id = ?' => $id));
        //return $this->_departmentToSubdepartments($rows);
        return $this->_departmentToSubdepartments($rows);
    }
    
    /**
     * Alle departmentsIds en subdepartments ophalen
     * 
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_departmentToSubdepartments($rows);
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_DepartmentHasSubdepartment   
     */
    private function _departmentToSubdepartment(Zend_Db_Table_Row_Abstract $row = null)
    {
        $departmentHasSubdepartment = new Application_Model_DepartmentHasSubdepartment();
        //$SubdepartmentMapper        = new Application_Model_SubdepartmentMapper();
        
        
        
        if ($row) {                        
            $departmentHasSubdepartment->DepartmentId       = $row['department_id'];                        
            $departmentHasSubdepartment->subdepartmentId    = $row['subdepartments_id'];                       
            $departmentHasSubdepartment->SubdepartmentPoints= $row['subdepartments_points'];                   
            }
        
        return $departmentHasSubdepartment;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _departmentToSubdepartments(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $departmentHasSubdepartments = array();

        if ($rows) {
            foreach ($rows as $row) {
                $departmentHasSubdepartments[] = $this->_departmentToSubdepartment($row);
            }
        }
        
        return $departmentHasSubdepartments;
    }

}


